package com.yoomiito.app.ui.anyview.order;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.utils.DensityUtil;

/**
 * 用于显示抵扣 以前显示优米 后改成可抵扣余额
 */
public class DeductionView extends LinearLayout {
    private TextView mValueTv;
    private Context mContext;
    private View mTipView;
    private PopupWindow mPopupWindow;
    private String value = "0";
    private String youmiSize = "0";
    public DeductionView(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_deduction,this,true);
        initView();
    }

    private void initView() {
        findViewById(R.id.small_tip).setOnClickListener(v -> {
            shopTip(v);
        });
        mValueTv = findViewById(R.id.value);
        setValue(value);
    }

    public void setValue(String value){
        this.value = value;
        mValueTv.setText("-"+value);
    }

    private void shopTip(View view){
        if (mTipView==null){
            mTipView = LayoutInflater.from(mContext).inflate(R.layout.small_tip_1,null,false);
        }
        TextView tipTv = mTipView.findViewById(R.id.tip);
        tipTv.setText("账户可抵扣余额：¥"+youmiSize+"\n1.玩优米送多多，送赠品即可获得可抵扣余额\n2.余额抵扣时必须足额抵扣，不得部分抵扣");
        if (mPopupWindow==null){
            mPopupWindow = new PopupWindow(mContext);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setContentView(mTipView);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0]+view.getWidth()+DensityUtil.dp2px(5), location[1]- DensityUtil.dp2px(5));
    }

    public void setYoumiSize(String youmiSize) {
        this.youmiSize = youmiSize;
    }
}
